package com.example.microinvestuser.service.user.impl;
import com.alibaba.fastjson.JSONObject;
import com.example.microinvestcommon.entity.UserStockSummary;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.util.ThreadLocalUtil;
import com.example.microinvestcommon.vo.*;
import com.example.microinvestuser.dao.UserGetMapper;
import com.example.microinvestuser.service.user.UserGetService;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserGetServiceImpl implements UserGetService {

    private final UserGetMapper userGetMapper;

    @Autowired
    public UserGetServiceImpl(UserGetMapper userGetMapper){
        this.userGetMapper=userGetMapper;
    }

    //返回用户信息
    @Override
    public Result<UsersVo> getUserInfo() {
        Map<String,Long> map = ThreadLocalUtil.get();
        Long userId=map.get("userId");
        UsersVo usersVo=userGetMapper.getUserInfo(userId);
        return Result.success(usersVo);
    }

    //返回用户收藏信息
    @Override
    public Result<List<UserStockFavoritesVo>> getUserStockFavorites() {
        Map<String,Long> map = ThreadLocalUtil.get();
        System.out.println(map);
        Long userId=map.get("userId");
        List<UserStockFavoritesVo> userStockFavorites=userGetMapper.getUserStockFavorites(userId);
        return Result.success(userStockFavorites);
    }

    //返回用户购买信息
    @Override
    public Result<List<BuyStockVo>> getUserBuyStock() {
        Map<String,Long> map =ThreadLocalUtil.get();
        Long userId=map.get("userId");
        List<BuyStockVo> userBuyStockInfo=userGetMapper.getUserBuyStockInfo(userId);
        return Result.success(userBuyStockInfo);
    }

    //返回用户卖出信息
    @Override
    public Result<List<SellStockVo>> getUserSellStock() {
        Map<String,Long> map =ThreadLocalUtil.get();
        Long userId=map.get("userId");
        List<SellStockVo> userSellStockInfo=userGetMapper.getUserSellStockInfo(userId);
        return Result.success(userSellStockInfo);
    }

    //返回总订单信息
    @Override
    public Result<List<UserOrderVo>> getUserOrder() {
        Map<String,Long> map =ThreadLocalUtil.get();
        Long userId=map.get("userId");
        List<UserOrderVo> userOrderVos=userGetMapper.getUserOrder(userId);
        return Result.success(userOrderVos);
    }

    /**
     *
     * @param stockSymbol
     * @return userOrderVos
     */
    @Override
    public Result<List<UserOrderVo>> getUserOrderByStockSymbol(String stockSymbol) {
        Map<String,Long> map =ThreadLocalUtil.get();
        Long userId=map.get("userId");
        List<UserOrderVo> userOrderVos=userGetMapper.getUserOrderByStockSymbol(userId,stockSymbol);
        return Result.success(userOrderVos);
    }

    /**
     *
     * @param searchString
     * @return <List<StockNumSymbolVo>
     */
    @Override
    public Result<List<StockNumSymbolVo>> userSearchSymbol(String searchString) {
        String keyword = "%" + searchString + "%";
        List<StockNumSymbolVo> resultList = userGetMapper.userSearchSymbol(keyword);
        return Result.success(resultList);
    }

    /**
     *
     * @param userId
     * @param stockSymbol
     * @return
     */
    @Override
    public Result isStarThisStock(Long userId, String stockSymbol) {
        Boolean b=userGetMapper.isUserStarThisStock(userId,stockSymbol);
        return Result.success(b);
    }

    /**
     *
     * @return
     */
    @Override
    public Result<List<UserStockSummaryVo>> getUserStockSummary() {
        Map<String,Long> map =ThreadLocalUtil.get();
        Long userId=map.get("userId");
        List<UserStockSummaryVo> userStockSummaryVoList=new ArrayList<>();
        String str=userGetMapper.getUserStockSummaryToString(userId);

        ObjectMapper mapper = new ObjectMapper();

        try {
            Map<String, UserStockSummary> dataMap = mapper.readValue(str, new TypeReference<Map<String, UserStockSummary>>() {});
            dataMap.forEach((k,v)->{
                UserStockSummaryVo userStockSummaryVo=new UserStockSummaryVo(k,v.getTotal_quantity(),v.getTotal_buy_amount());
                userStockSummaryVoList.add(userStockSummaryVo);
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return Result.error("json解析失败: "+e.getMessage());
        }

        return Result.success(userStockSummaryVoList);
    }
}
